import BindModel from './bindModel';
import BindAction from './bindAction';
import renderComponent from './render';
import global from './global';


export default class App {
    constructor() {
        this.data = {
            modelInstance: {},
            actionInstance: {}
        };

        this.bindModel = BindModel(this.setData, this.getData);
        this.bindAction = BindAction(this.setData, this.getData);
        this.render = renderComponent(this.setData, this.getData, global);
        this.getModel = this.getInstance('modelInstance');
        this.getAction = this.getInstance('actionInstance');
        this.removeModel = this.removeInstance('modelInstance');
        this.removeAction = this.removeInstance('actionInstance');
    }

    static bindGlobalModel = global.bindModel
    static bindGlobalAction = global.bindAction

    setData = (key, value) => {
        this.data[key] = value;
    }

    getData = (key) => {
        if (key) {
            return this.data[key]
        }
    
        return this.data;
    }

    getInstance = (type) => {
        return (modelName) => {
            if (modelName) {
                return this.getData(type)[modelName];
            }
            return null;
        }
    }

    removeInstance = (type) => {
        return (name) => {
            if (name) {
                let instance = this.getData(type);
                delete instance[name];
                this.setData({
                    [type]: instance
                });
            }
        }
    }
}